import axios from 'axios'
import { useGlobalStore } from '../stores/global'
const globalStore = useGlobalStore()
const params = {...globalStore.linkParams}
console.log('【请求 ----> request/index.ts ----> params】', params)

export const service = axios.create({
  baseURL: params.baseURL.http_api,
  timeout: 15000
})

service.interceptors.request.use(
  (config) => {
    if (config.method === 'post') {
      config.data.uid = params.uid
      config.data.token = params.token
      config.data.lang = params.lang
      config.data.actName = params.actName
      config.data.season_name = params.season_name
    }    
    console.log('【请求拦截---config：】', config)
    return config
  },
  (error) => Promise.reject(error)
)

service.interceptors.response.use(
  (response) => {
    console.log('【请求方式：】', response.config.method)
    console.log('【请求响应结果：】', response)
    // const res = response.data
    // if (res.code !== 0) {
    //   console.error('接口错误:', res.message)
    //   return Promise.reject(res.message)
    // }
    // return res.data
    if(response.status!==200){
      console.error('接口错误:')
      return Promise.reject('接口错误:')
    }
    return response
  },
  (error) => {
    console.error('网络错误:', error)
    return Promise.reject(error)
  }
)
